<?php
/*
  $Program: ECJC Online Shop
  $Id: price_updater.php
  $Based on: osCommerce, Open Source E-Commerce Solutions
  Released under the GNU General Public License
*/
  defined( 'JOSCOM_VERSION' ) or die( 'Direct Access to this location is not allowed.' );
  if (!($acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'all' )
  | $acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'com_banners' ))) {
    //j15nc
  }
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $cPath, $cPath_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    require(DIR_WS_COMPONENT . 'includes/application_top.php');
  }

// Functions to fill the dropdown boxes
  function tep_get_manufacturers($manufacturers_array = '') { // Function borrowed from the Catalog side
    if (!is_array($manufacturers_array)) $manufacturers_array = array();
    $manufacturers_query = tep_db_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS . " order by manufacturers_name");
    while ($manufacturers = tep_db_fetch_array($manufacturers_query)) {
      $manufacturers_array[] = array('id' => $manufacturers['manufacturers_id'], 'text' => $manufacturers['manufacturers_name']);
    }
    return $manufacturers_array;
  }

  function tep_get_categories($categories_array = '') { // Function modified from tep_get_manufacturers()
    global $language;
    if (!is_array($categories_array)) $categories_array = array();
    $categories_query = tep_db_query("SELECT categories_id, 
	                                         categories_name
                                      FROM " . TABLE_CATEGORIES_DESCRIPTION . " cd, 
                                           " . TABLE_LANGUAGES . " l 
									  WHERE l.languages_id = cd.language_id
									    AND l.name = '" . $language . "' 
									  ORDER BY categories_name"
									);
    while ($categories = tep_db_fetch_array($categories_query)) {
      $categories_array[] = array('id' => $categories['categories_id'], 'text' => $categories['categories_name']);
    }
    return $categories_array;
  }
  

  function tep_get_models($models_array = '') { // Function modified from tep_get_manufacturers()
    global $language, $first, $last;
    if (!is_array($models_array)) $models_array = array();
    $models_query = tep_db_query("SELECT products_id,
	                                     products_model 
                                  FROM " . TABLE_PRODUCTS . " 
                                  ORDER BY products_model"
								);
    $count = 0;
    while ($models = tep_db_fetch_array($models_query)) {
	  if ($count == 0) {
	    $first = $models['products_model'];  
	  }
      $models_array[] = array('id' => $models['products_model'], 'text' => $models['products_model']);
	  $count++;
	  $last = $models['products_model'];
    }
	
    return $models_array;
  }


  $models_array = tep_get_models();
  $from = $first;
  $to = $last;
  

// Process the request data
  if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'update') {

// Get the data from the form and sanitize it
    if (isset($_REQUEST['manufacturers_id'])) {
      $mfr = (int)$_REQUEST['manufacturers_id']; 
    } else {
      $mfr = 0;
    }

    if (isset($_POST['categories_id'])) {
      $cat = (int)$_POST['categories_id']; 
    } else {
      $cat = 0;
    }

    if (isset($_POST['from_id'])) {
      $from = $_POST['from_id']; 
    }

    if (isset($_POST['to_id'])) {
      $to = $_POST['to_id']; 
    }

    if (isset($_POST['like'])) {
      $like = $_POST['like']; 
    } else {
      $like = '';
    }

    if (isset($_POST['add'])) {
      $add = (int)$_POST['add']; 
    } else {
      $add = 1;
    }

    if (isset($_POST['fixed'])) {
      $fixed = (int)$_POST['fixed']; 
    } else {
      $fixed = 1;
    }

    if (isset($_POST['value'])) {
       $value = ereg_replace('[^0-9.]', '', $_POST['value']); 
    } else {
      $value = 0;
    }
	
	
// Set the SQL where function 
    if ($mfr == 0) {
	  if ($cat == 0) {
        $where_string = '';
	  } else {
        $where_string = ' AND pcat.categories_id = ' . $cat;
	  }
    } else {
      $where_string = ' AND manufacturers_id=' . $mfr; 
	  if ($cat != 0) {
        $where_string .= ' AND pcat.categories_id = ' . $cat;
	  }
    }
	
    if ($like == '') {
	  if ($from != $first) {
        $where_string .= " AND p.products_model >= '" . $from . "'"; 
	  }
	  if ($to != $last) {
        $where_string .= " AND p.products_model <= '" . $to . "'";
      } 
    } else {
      $where_string .= " AND p.products_model LIKE '" . $like . "'";
	}
  
// Query to get the selected products and make the changes
    $products_update_query = tep_db_query('SELECT p.products_id AS id, 
                                                  p.products_price AS price 
                                           FROM ' . TABLE_PRODUCTS . ' p, ' . 
                                                    TABLE_PRODUCTS_TO_CATEGORIES . ' pcat 
                                           WHERE p.products_id = pcat.products_id' . 
                                           $where_string
                                         );
    $count = 0;
    while ($products_update = tep_db_fetch_array($products_update_query)) {
      if ($fixed == 0) {  // Fixed price change
	    if ($add == 0) {  // Subtract
          $new_price = $products_update['price'] - $value;
	    } else {  // Add
          $new_price = $products_update['price'] + $value;
        }  
      } else {  // Percent change
	    if ($add == 0) {  // Subtract
          $new_price = $products_update['price'] * (1 - ($value / 100));
	    } else {  // Add
          $new_price = $products_update['price'] * (1 + ($value / 100));
        }  
	  }
      tep_db_query("UPDATE " . TABLE_PRODUCTS . " 
	                SET products_price='" . $new_price . "' 
                    WHERE products_id='" . $products_update['id'] . "'"
                  );
      $count++;
    }  // Products while loop
	

// If a manufacturer was selected, get the name
	if ($mfr != 0) {
	  $manufacturers_query = tep_db_query("SELECT manufacturers_name FROM " . TABLE_MANUFACTURERS . " WHERE manufacturers_id=" . $mfr);
      $manufacturers = tep_db_fetch_array($manufacturers_query);
      $manufacturer = $manufacturers['manufacturers_name'];
    } else {
      $manufacturer = TEXT_ALL_MANUFACTURERS;
    }
	
	
// If a category was selected, get the name
    if ($cat != 0) {
      $categories_query = tep_db_query("SELECT cd.categories_name
                                        FROM " . TABLE_CATEGORIES_DESCRIPTION . " cd, 
                                             " . TABLE_LANGUAGES . " l 
									    WHERE l.languages_id = cd.language_id
									      AND l.name = '" . $language . "' 
									      AND categories_id = " . $cat);
      $categories = tep_db_fetch_array($categories_query);
      $category = TEXT_THE . $categories['categories_name'] . TEXT_CATEGORY;
    } else {
      $category = TEXT_ALL_CATEGORIES;
    }
	
	
// Finish the rest of the update text information
    $fixed_string = '';
    if ($fixed == 1) {
      $fixed_string = TEXT_PERCENT;
    }
	
    $add_string = TEXT_DECREASED_BY;
    if ($add == 1) {
    $add_string = TEXT_INCREASED_BY;
    }
	
    $update_string = $manufacturer . TEXT_PRICES_IN . $category . TEXT_WERE . $add_string . $value . $fixed_string;

  } // End action=update
 
?>

<link rel="stylesheet" type="text/css" href="<?php echo DIR_WS_COMPONENT ?>includes/stylesheet.css">
<script language="javascript" src="<?php echo DIR_WS_COMPONENT ?>includes/general.js"></script>
</head>

<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading">Product Price Updater</td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td>
            <table border=0 width=700 cellspacing=0 cellpadding=5>
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_UPDATES; ?>&nbsp;</td>
              </tr>
              <tr class="dataTableRow">
                <td class="dataTableContent">
<?php
   if (isset($count) && $count != 0) {
    echo $update_string;
  }
?>
                </td>
              </tr>
              <tr class="dataTableRow">
                <td class="dataTableContent">
<?php
  if (isset($count) && $count != 0) {
    echo $count;
  } else {
    echo TEXT_NO;
  }
  echo TEXT_PRODUCTS_UPDATED;
?>
				</td>
              </tr>
            </table>
        </td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', "100%", 20); ?></td>
      </tr>
      <tr>
        <td><!--
          <form action="price_updater.php?action=update" method=post>
          -->
          <? echo tep_draw_form('pupdate', tep_href_link(FILENAME_PRICE_UPDATER, 'action=update')); ?>
            <table border=0 width=700 cellspacing=0 cellpadding=2>
              <tr>
                <td colspan=7><?php echo tep_black_line(); ?></td>
              </tr>
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_MANUFACTURER; ?>&nbsp;</td>
                <td colspan=2 class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_CATEGORY; ?>&nbsp;</td>
                <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_PLUS_MINUS; ?>&nbsp;</td>
                <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_FIXED; ?>&nbsp;</td>
                <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_VALUE; ?>&nbsp;</td>
                <td class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
              <tr>
                <td colspan=7><?php echo tep_black_line(); ?></td>
              </tr>
              <tr class=attributes-odd>
                <td class="smallText">&nbsp;<?php echo tep_draw_pull_down_menu('manufacturers_id', tep_get_manufacturers(array(array('id' => '0', 'text' => 'All Manufacturers ')))); ?>&nbsp;</td>
                <td colspan=2 class="smallText">&nbsp;<?php echo tep_draw_pull_down_menu('categories_id', tep_get_categories(array(array('id' => '0', 'text' => 'All Categories ')))); ?>&nbsp;</td>
                <td class="smallText">&nbsp;<?php echo tep_draw_pull_down_menu('add', array(array('id' => '1', 'text' => '+'), array('id' => '0', 'text' => '-'))); ?>&nbsp;</td>
                <td class="smallText">&nbsp;<?php echo tep_draw_pull_down_menu('fixed', array(array('id' => '1', 'text' => '%'), array('id' => '0', 'text' => 'Fixed'))); ?>&nbsp;</td>
                <td class="smallText">&nbsp;<?php echo tep_draw_input_field('value', "0"); ?>&nbsp;</td>
                <td class="smallText">&nbsp;<?php echo tep_image_submit('button_update.gif', IMAGE_UPDATE); ?>&nbsp;</td>
              </tr>
              <tr>
                <td colspan=7><?php echo tep_black_line(); ?></td>
              </tr>
              <tr>
                <td colspan=7><?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?></td>
              </tr>
              <tr>
                <td colspan=7><?php echo tep_black_line(); ?></td>
              </tr>
              <tr class="dataTableHeadingRow">
                <td colspan=7 class="dataTableHeadingContent">&nbsp;<?php echo TABLE_HEADING_MODEL; ?>&nbsp;</td>
              </tr>
              <tr>
                <td colspan=7><?php echo tep_black_line(); ?></td>
              </tr>
              <tr class=attributes-odd>
                <td class="smallText">&nbsp;<?php echo tep_draw_pull_down_menu('from_id', $models_array, $from); ?>&nbsp;&nbsp;&nbsp;<?php echo TEXT_TO ?>&nbsp;</td>
                <td class="smallText">&nbsp;<?php echo tep_draw_pull_down_menu('to_id', $models_array, $to); ?>&nbsp;</td>
                <td class="smallText">&nbsp;Like:&nbsp;</td>
                <td colspan=4 class="smallText">&nbsp;<?php echo tep_draw_input_field('like', ""); ?>&nbsp;&nbsp;&nbsp;<?php echo TEXT_NOTES ?>&nbsp;</td>
              </tr>
              <tr>
                <td colspan=7><?php echo tep_black_line(); ?></td>
              </tr>
            </table>
          </form>
        </td>
      </tr>
    </table></td>
  </tr>
</table>

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->

<?php
  require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
?>
